package com.yuchen.ishop.seller;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.MyPager;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Seller;
import com.yuchen.ishop.entity.SellerXiaoer;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.security.PasswordUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 *
 */
@Transactional
@Path("xiaoerService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class XiaoerServiceImpl implements XiaoerService {

    @Autowired
    private BaseDao dao;

    @GET
    @Path("existXiaoer/{sellerId}/{id}")
    @Transactional(readOnly = true)
    public Responses existXiaoer(@PathParam("sellerId") Long sellerId, @PathParam("id") Long id) {
        Condition c = Cnd.where("id", "=", id).and("sellerId", "=", sellerId);
        SellerXiaoer xiaoer = dao.fetch(SellerXiaoer.class, c);
        return xiaoer != null ? ResponseUtils.success() : ResponseUtils.error();
    }

    @GET
    @Path("findXiaoer/{id}")
    @Transactional(readOnly = true)
    public SellerXiaoer findXiaoer(@PathParam("id") Long id) {
        return dao.fetch(SellerXiaoer.class, id);
    }

    @POST
    @Path("updatePwd")
    public Responses updatePwd(Map map) {
        if (map != null && map.size() == 2 && map.get("id")!=null && map.get("password")!=null) {
            String password = (String) map.get("password");
            Map m = PasswordUtils.genMD5Str(password);
            SellerXiaoer xiaoer = new SellerXiaoer();
            xiaoer.setId(Long.valueOf(map.get("id").toString()));
            xiaoer.setSalt((String) m.get("salt"));
            xiaoer.setPassword((String) m.get("password"));
            dao.updateIgnoreNull(xiaoer);
            return ResponseUtils.success();
        }
        return ResponseUtils.error();
    }


    @POST
    @Path("insertXiaoer")
    public SellerXiaoer insertXiaoer(SellerXiaoer sellerXiaoer) {
        return dao.insert(sellerXiaoer);
    }

    @POST
    @Path("updateXiaoer")
    public Responses updateXiaoer(SellerXiaoer sellerXiaoer) {
        dao.updateIgnoreNull(sellerXiaoer);
        return ResponseUtils.success();
    }

    @DELETE
    @Path("deleteXiaoer/{id}")
    public Responses deleteXiaoer(@PathParam("id") Long id) {
        dao.delete(SellerXiaoer.class, id);
        return ResponseUtils.success();
    }

    @POST
    @Path("findXiaoers")
    @Transactional(readOnly = true)
    public MyPager findXiaoers(Map params) {
        Criteria cri = Cnd.cri();
        MyPager myPager = new MyPager();
        if (params != null && params.size() > 0) {
            if (params.get("pageSize") != null)
                myPager.setPageSize(NumberUtils.toInt(params.get("pageSize").toString()));
            if (params.get("pageNo") != null)
                myPager.setPageNo(NumberUtils.toInt(params.get("pageNo").toString()) );
            if (params.get("sellerId") != null) {
                cri.where().andEquals("sellerId", params.get("sellerId"));
            }
        }

        cri.getOrderBy().desc("createTime");
        return dao.queryPager(SellerXiaoer.class, cri, myPager);
    }
}
